﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.Modules.Demo;
using System.Data;


namespace Twi.Pages.Demo
{
    public class PageCommon
    {
        public bool ExistField(string tableName, string fieldName, string fieldValue)
        {
            return ModuleCommon.Instance.ExistField(tableName, fieldName, fieldValue);
        }

        public bool ExistField(string tableName, string fieldName, string fieldValue, string id, string idFieldName = "FID")
        {
            return ModuleCommon.Instance.ExistField(tableName, fieldName, fieldValue, id, idFieldName);
        }


        public bool ExistEditSort(string tableName, int? sort, string id, string filterField, string filterFieldValue, string sortField = "FSort", string idField = "FID")
        {
            return ModuleCommon.Instance.ExistEditSort(tableName, sort, id, filterField, filterFieldValue, sortField, idField);
        }

        public bool ExistEditSort(string tableName, int? sort, string id, string sortField = "FSort", string idField = "FID")
        {
            return ModuleCommon.Instance.ExistEditSort(tableName, sort, id, sortField, idField);
        }

        public bool ExistSort(string tableName, int? sort, string filterField, string filterFieldValue, string sortField = "FSort")
        {
            return ModuleCommon.Instance.ExistSort(tableName, sort, filterField, filterFieldValue, sortField);
        }

        public bool ExistSort(string tableName, int? sort, string sortField = "FSort")
        {
            return ModuleCommon.Instance.ExistSort(tableName, sort, sortField);
        }

        public int GetMaxSort(string tableName, string filterField, string filterFieldValue, string sortField = "FSort")
        {
            return ModuleCommon.Instance.GetMaxSort(tableName, filterField, filterFieldValue, sortField);
        }

        public int GetMaxSort(string tableName, string sortField = "FSort")
        {
            return ModuleCommon.Instance.GetMaxSort(tableName, sortField);
        }



        public void Delete(string tableName, string[] ids, string idField = "FID")
        {
            ModuleCommon.Instance.Delete(tableName, ids, idField);
        }

        public void Delete(string tableName, string id, string idField = "FID")
        {

            ModuleCommon.Instance.Delete(tableName, id, idField);
        }

        public DataTable GetAll(string tableName)
        {
            return ModuleCommon.Instance.GetAll(tableName);
        }

        public DataTable GetBySimpleCondition(string tableName, Dictionary<string, object> dictCondition, SimpleCondition simpleCondition, string selectFields = "*")
        {
            return ModuleCommon.Instance.GetBySimpleCondition(tableName, dictCondition,simpleCondition,selectFields);
        }

        public DataTable GetPagingDataByCondition(int start, int limit, string twiData)
        {
            return ModuleCommon.Instance.GetPagingDataByCondition(start, limit, twiData);
        }

        public TwiReturn NewOrEdit(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.id = ModuleCommon.Instance.NewOrEdit(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn SimpleNew(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.id = ModuleCommon.Instance.SimpleNew(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
        /// <summary>
        /// 加一个Dict前缀是为了防止前台调用目标不明确
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dictFields"></param>
        /// <returns></returns>
        public TwiReturn DictSimpleNew(string tableName, Dictionary<string, object> dictFields)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                ModuleCommon.Instance.SimpleNew(tableName, dictFields);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn SimpleEdit(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                ModuleCommon.Instance.SimpleEdit(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
        /// <summary>
        /// 加一个Dict前缀是为了防止前台调用目标不明确
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dictUpdateFields"></param>
        /// <param name="dictConditionFields"></param>
        /// <returns></returns>
        public TwiReturn DictSimpleEdit(string tableName, Dictionary<string, object> dictUpdateFields, Dictionary<string, object> dictConditionFields)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                ModuleCommon.Instance.SimpleEdit(tableName, dictUpdateFields, dictConditionFields);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }


    }
}
